﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using KHMI.Attributes;

namespace DrawTools
{
    [Serializable]
    public class DrawText : DrawRectangle
    {
        public int Cube { get; set; }

        [ExtendCategoryAttribute("扩展设置", VectorName = "内容")]
        public string ShowText { get; set; }  //显示内容

        private Font _font;

        [ExtendCategoryAttribute("扩展设置", VectorName = "字体")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }

        public DrawText()
            : this(0, 0, 1, 1)
        {
            ShowText = "编辑文字";
        }

        public DrawText(int x, int y, int width, int height)
            : base()
        {
            _font = new Font("Verdana", 12);
            ShowText = "编辑文字";
            Rectangle = new Rectangle(x, y, width, height);
            Initialize();
        }

        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            info.AddValue("cube" + orderNumber.ToString(), Cube);
            info.AddValue("ShowText" + orderNumber.ToString(), ShowText);
            info.AddValue("ShowFont" + orderNumber.ToString(), _font);
            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            Cube = info.GetInt32("cube" + orderNumber.ToString());
            ShowText = info.GetString("ShowText" + orderNumber.ToString());
            _font = (Font)info.GetValue("ShowFont" + orderNumber.ToString(), _font.GetType());
            base.LoadFromStream(info, orderNumber);
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }


        public override void Draw(Graphics g)
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff"));
                Pen pen = new Pen(Color, PenWidth);
                GraphicsPath gp = new GraphicsPath();
                StringFormat format = StringFormat.GenericDefault;
                gp.AddString(ShowText, _font.FontFamily, (int)_font.Style, _font.SizeInPoints,
                             new PointF(Rectangle.X, Rectangle.Y), format);

                if (Rotation != 0)
                {
                    RectangleF pathBounds = gp.GetBounds();
                    Matrix m = new Matrix();
                    m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width / 2),
                        pathBounds.Top + (pathBounds.Height / 2)),
                               MatrixOrder.Append);
                    gp.Transform(m);
                }
                g.DrawPath(pen, gp);
                pen.Dispose();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}
